//
//  MineOrderTableViewCell.m
//  B2C
//
//  Created by 李小龙 on 2017/7/31.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "MineOrderTableViewCell.h"

#define OrderButtonBaseTag 123

@implementation MineOrderTableViewCell
#pragma mark - 订单列表
+ (instancetype)mineOrderCellWith:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath count:(NSInteger)count {
    NSInteger index = 0;
    NSString *identifier = @"";
    if (indexPath.row == 0) {
        index = 0;
        identifier = @"mineOrderCellFirst";
    } else if (indexPath.row == count+1) {
        index = 2;
        identifier = @"mineOrderCellThird";
    } else if (indexPath.row == count+2) {
        index = 3;
        identifier = @"mineOrderCellFourth";
    } else {
        index = 1;
        identifier = @"mineOrderCellSecond";
    }
    MineOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MineOrderTableViewCell" owner:self options:nil] objectAtIndex:index];
    }
    return cell;
}
- (void)updateCellWith:(OrderModel *)model indexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        // 时间、状态
        [self.firstNameLabel setText:[DateSomeThing getDateStrWithTimestampstr:model.createtime formatStr:@"yyyy-MM-dd"]];
//        if ([model.shop.sid isEqualToString:@"0"]) {
//            // 自营
//            [self.firstIconImageView setImage:[UIImage imageNamed:@"logo_small"]];
//            [self.firstTypeImageView setImage:[UIImage imageNamed:@"self_support"]];
//        } else {
//            [self.firstIconImageView setImage:[UIImage imageNamed:@"shopicon"]];
//            if ([model.shop.stype isEqualToString:@"1"]) {
//                // 企业
//                [self.firstTypeImageView setImage:[UIImage imageNamed:@"enterprise"]];
//            } else {
//                // 个人
//                [self.firstTypeImageView setImage:[UIImage imageNamed:@"personal"]];
//            }
//        }
        
        // 状态0已提交，1已付款，2已发货，3已签收，4已评价，5已取消，6已删除，7退款申请，8已退款
        if (model.isrefund) {
            [self.firstStatusLabel setText:@"退款申请"];
        } else {
            switch (model.omstatus.intValue) {
                case 0:
                    [self.firstStatusLabel setText:@"等待买家付款"];
                    break;
                case 1:
                    [self.firstStatusLabel setText:@"等待发货"];
                    break;
                case 2:
                    [self.firstStatusLabel setText:@"已发货"];
                    break;
                case 3:
                    [self.firstStatusLabel setText:@"已完成"];
                    break;
                case 4:
                    [self.firstStatusLabel setText:@"已评价"];
                    break;
                case 5:
                    [self.firstStatusLabel setText:@"已取消"];
                    break;
                case 6:
                    [self.firstStatusLabel setText:@"已删除"];
                    break;
                case 7:
                    [self.firstStatusLabel setText:@"已退款"];
                    break;
                case 8:
                    [self.firstStatusLabel setText:@"退款申请"];
                    break;
                default:
                    break;
            }
        }
        
    } else if (indexPath.row == model.ordergoods.count+1) {
        NSString *totalStr, *priceStr;
        if (model.omtype.integerValue == 1) {
            totalStr = [NSString stringWithFormat:@"共计%@件商品，实付积分：",model.omnumber];
            priceStr = [NSString stringWithFormat:@"%ld",model.omprice.integerValue];
        } else {
            totalStr = [NSString stringWithFormat:@"共计%@件商品，实付款：",model.omnumber];
            priceStr = [NSString stringWithFormat:@"¥%@",model.omprice];
        }
        [self.thirdTotalLabel setText:totalStr];
        [self.thirdPriceLabel setText:priceStr];
    } else if (indexPath.row == model.ordergoods.count+2) {
        // 状态0已提交，1已付款，2已发货，3已签收，4已评价，5已取消，6已删除，7退款申请，8已退款
        CGFloat buttonY = 9;
        CGFloat buttonW = 75;
        CGFloat kong = 5;
        [self removeFourthBackViewSubView];
        if (model.isrefund) {
            // 查看详情
            OrderHalfButton *button = [OrderHalfButton orderButtonWithType:OrderButtonTypeRefundDetail frame:CGRectMake(ScreenWidth-(kong+buttonW), buttonY, buttonW, self.fourthBackView.height-buttonY*2) buttonTitle:@"查看详情" color:[UIColor blackTextColor] borderColor:[UIColor grayTextColor]];
            [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [button setTag:indexPath.section+OrderButtonBaseTag];
            [self.fourthBackView addSubview:button];
        } else {
            switch (model.omstatus.intValue) {
                case 0:
                {
                    for (NSInteger i = 1; i <= 2; i++) {
                        CGRect frame = CGRectMake(ScreenWidth-(kong+buttonW)*i, buttonY, buttonW, self.fourthBackView.height-buttonY*2);// self.fourthBackView.width
                        
                        OrderButtonType type;
                        NSString *buttonTitle;
                        UIColor *color, *borderColor;
                        if (i == 1) {
                            type = OrderButtonTypePay;
                            buttonTitle = @"付款";
                            color = [UIColor ThemeColor];
                            borderColor = [UIColor ThemeColor];
                        } else {
                            type = OrderButtonTypeCancel;
                            buttonTitle = @"取消订单";
                            color = [UIColor blackTextColor];
                            borderColor = [UIColor grayTextColor];
                        }
                        
                        OrderHalfButton *button = [OrderHalfButton orderButtonWithType:type frame:frame buttonTitle:buttonTitle color:color borderColor:borderColor];
                        [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                        [button setTag:indexPath.section+OrderButtonBaseTag];
                        [self.fourthBackView addSubview:button];
                        
                    }
                } break;
                case 1:
                {
                    OrderHalfButton *button = [OrderHalfButton orderButtonWithType:OrderButtonTypeAlert frame:CGRectMake(ScreenWidth-(kong+buttonW), buttonY, buttonW, self.fourthBackView.height-buttonY*2) buttonTitle:@"提醒发货" color:[UIColor ThemeColor] borderColor:[UIColor ThemeColor]];
                    [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                    [button setTag:indexPath.section+OrderButtonBaseTag];
                    [self.fourthBackView addSubview:button];
                } break;
                case 2:
                {
                    for (NSInteger i = 1; i <= 2; i++) {
                        CGRect frame = CGRectMake(ScreenWidth-(kong+buttonW)*i, buttonY, buttonW, self.fourthBackView.height-buttonY*2);// self.fourthBackView.width
                        
                        OrderButtonType type;
                        NSString *buttonTitle;
                        UIColor *color, *borderColor;
                        if (i == 1) {
                            type = OrderButtonTypeSure;
                            buttonTitle = @"确认收货";
                            color = [UIColor ThemeColor];
                            borderColor = [UIColor ThemeColor];
                        } else {
                            type = OrderButtonTypeCheck;
                            buttonTitle = @"查看物流";
                            color = [UIColor blackTextColor];
                            borderColor = [UIColor grayTextColor];
                        }
                        
                        OrderHalfButton *button = [OrderHalfButton orderButtonWithType:type frame:frame buttonTitle:buttonTitle color:color borderColor:borderColor];
                        [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                        [button setTag:indexPath.section+OrderButtonBaseTag];
                        [self.fourthBackView addSubview:button];
                        
                    }
                } break;
                case 3:
                {
                    for (NSInteger i = 1; i <= 3; i++) {
                        
                        CGRect frame = CGRectMake(ScreenWidth-(kong+buttonW)*i, buttonY, buttonW, self.fourthBackView.height-buttonY*2);// self.fourthBackView.width
                        
                        OrderButtonType type;
                        NSString *buttonTitle;
                        UIColor *color, *borderColor;
                        if (i == 1) {
                            type = OrderButtonTypeEvaluate;
                            buttonTitle = @"评价";
                            color = [UIColor ThemeColor];
                            borderColor = [UIColor ThemeColor];
                        } else if (i == 2) {
                            type = OrderButtonTypeDelete;
                            buttonTitle = @"删除订单";
                            color = [UIColor blackTextColor];
                            borderColor = [UIColor grayTextColor];
                        } else {
                            type = OrderButtonTypeCheck;
                            buttonTitle = @"查看物流";
                            color = [UIColor blackTextColor];
                            borderColor = [UIColor grayTextColor];
                        }
                        
                        OrderHalfButton *button = [OrderHalfButton orderButtonWithType:type frame:frame buttonTitle:buttonTitle color:color borderColor:borderColor];
                        [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                        [self.fourthBackView addSubview:button];
                        
                    }
                } break;
                case 4:
                {
                    for (NSInteger i = 1; i <= 2; i++) {
                        CGRect frame = CGRectMake(ScreenWidth-(kong+buttonW)*i, buttonY, buttonW, self.fourthBackView.height-buttonY*2);// self.fourthBackView.width
                        
                        OrderButtonType type;
                        NSString *buttonTitle;
                        UIColor *color, *borderColor;
                        if (i == 1) {
                            type = OrderButtonTypeDelete;
                            buttonTitle = @"删除订单";
                            color = [UIColor ThemeColor];
                            borderColor = [UIColor ThemeColor];
                        } else {
                            type = OrderButtonTypeCheck;
                            buttonTitle = @"查看物流";
                            color = [UIColor blackTextColor];
                            borderColor = [UIColor grayTextColor];
                        }
                        
                        OrderHalfButton *button = [OrderHalfButton orderButtonWithType:type frame:frame buttonTitle:buttonTitle color:color borderColor:borderColor];
                        [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                        [self.fourthBackView addSubview:button];
                        
                    }
                } break;
                case 5:
                {
                    OrderHalfButton *button = [OrderHalfButton orderButtonWithType:OrderButtonTypeDelete frame:CGRectMake(ScreenWidth-(kong+buttonW), buttonY, buttonW, self.fourthBackView.height-buttonY*2) buttonTitle:@"删除订单" color:[UIColor ThemeColor] borderColor:[UIColor ThemeColor]];
                    [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                    [self.fourthBackView addSubview:button];
                } break;
                case 6:
                {
                    
                } break;
                case 7:
                {
                    OrderHalfButton *button = [OrderHalfButton orderButtonWithType:OrderButtonTypeDelete frame:CGRectMake(ScreenWidth-(kong+buttonW), buttonY, buttonW, self.fourthBackView.height-buttonY*2) buttonTitle:@"删除订单" color:[UIColor ThemeColor] borderColor:[UIColor ThemeColor]];
                    [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                    [self.fourthBackView addSubview:button];
                } break;
                case 8:
                {
                    
                } break;
                default:
                    break;
            }
        }
        
    } else {
        OrdersonModel *goodModel = model.ordergoods[indexPath.row-1];
        
        [self.secondImageView sd_setImageWithURL:GetUrl(goodModel.gimg) placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];
        [self.secondNameLabel setText:goodModel.gname];
        //MARK:测试 gpricetype没有了
        [self.secondCountLabel setText:[NSString stringWithFormat:@"x%ld",(long)goodModel.ognumber.integerValue]];
        if (goodModel.isigoods) {
            [self.secondPriceLabel setText:[NSString stringWithFormat:@"%ld积分",goodModel.gintegral.integerValue]];
        } else {
            [self.secondPriceLabel setText:[NSString stringWithFormat:@"¥%@",goodModel.gprice]];
        }
        // 属性
        NSString *specs = @"";
        for (NSInteger i = 0; i < goodModel.otop.count; i++) {
            OtopsModel *otops = goodModel.otop[i];
            if (i == 0) {
                specs = otops.taname;
            } else {
                specs = [NSString stringWithFormat:@"%@;%@",specs,otops.taname];
            }
        }
        [self.secondSpecsLabel setText:specs];
        
//        if ([goodModel.gpricetype isEqualToString:@"0"]) {
//            NSString *allPrice;
//            if (TextIsNotNil(goodModel.goldprice)) {
//                allPrice = goodModel.goldprice;
//            } else {
//                double priceValue = goodModel.gprice.doubleValue * goodModel.gshare.doubleValue * 100;
//                allPrice = [NSString stringWithFormat:@"%.2f",round(priceValue)/100];
//            }
//            [self.secondCountLabel setText:@"x1"];
//            [self.secondPriceLabel setText:[NSString stringWithFormat:@"¥%@",allPrice]];
//        } else {
//            [self.secondCountLabel setText:[NSString stringWithFormat:@"x%ld",(long)goodModel.gshare.integerValue]];
//            [self.secondPriceLabel setText:[NSString stringWithFormat:@"¥%.2f",goodModel.gprice.doubleValue]];
//        }
        
//        if (indexPath.row == model.goods.count) {
//            [self.secondLineView setHidden:YES];
//        } else {
//            [self.secondLineView setHidden:NO];
//        }
    }
}
- (void)orderButtonAction:(OrderHalfButton *)button {
    if (self.orderBlock) {
        self.orderBlock(button.type, button.tag);
    }
}
#pragma mark - 退款订单列表
+ (instancetype)refundListCellWith:(UITableView *)tableView indexPath:(NSIndexPath *) indexPath count:(NSInteger)count {
    NSInteger index = 0;
    NSString *identifier = @"";
    if (indexPath.row == count) {
        index = 4;
        identifier = @"mineOrderCellFifth";
    } else if (indexPath.row == count+1) {
        index = 3;
        identifier = @"mineOrderCellFourth";
    } else {
        index = 1;
        identifier = @"mineOrderCellSecond";
    }
    MineOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MineOrderTableViewCell" owner:self options:nil] objectAtIndex:index];
    }
    return cell;
}
- (void)updateRefundListCellWith:(OrderModel *)model indexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {//model.goods.count
        NSString *str = [NSString stringWithFormat:@"退款金额：¥%@",model.refund.lastObject.rprice];
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
        [attributedStr setAttributes:@{NSFontAttributeName:TextPingFangFontSize(16),
                                       NSForegroundColorAttributeName:[UIColor colorWithHex:0x151515 alpha:1.0]} range:NSMakeRange(0, str.length)];
        [attributedStr setAttributes:@{NSFontAttributeName:TextPingFangFontSize(12),
                                       NSForegroundColorAttributeName:[UIColor blackTextColor]
                                       } range:[str rangeOfString:@"退款金额："]];
        [self.fifthPriceLabel setAttributedText:attributedStr];
        
        // 状态0已提交，1已付款，2已发货，3已签收，4已评价，5已取消，6已删除，7退款申请，8已退款
        if (model.isrefund) {
            switch (model.refund.lastObject.rstatus.intValue) {
                case 0:
                    [self.fifthStatusLabel setText:@"退款中"];
                    break;
                case 1:
                    [self.fifthStatusLabel setText:@"已退款"];
                    break;
                case 2:
                    [self.fifthStatusLabel setText:@"退款失败"];
                    break;
                case 3:
                    [self.fifthStatusLabel setText:@"退款驳回"];
                    break;
                default:
                    [self.fifthStatusLabel setText:@"退款申请"];
                    break;
            }
        } else {
            switch (model.omstatus.intValue) {
                case 7:
                    [self.fifthStatusLabel setText:@"退款中"];
                    break;
                case 8:
                    [self.fifthStatusLabel setText:@"已退款"];
                    break;
                default:
                    break;
            }
        }
        
    } else if (indexPath.row == 1+1) {//model.goods.count
        [self removeFourthBackViewSubView];
        
        CGFloat buttonY = 9;
        CGFloat buttonW = 75;
        CGFloat kong = 5;
        
        OrderHalfButton *button = [OrderHalfButton orderButtonWithType:OrderButtonTypeRefundDetail frame:CGRectMake(ScreenWidth-(kong+buttonW), buttonY, buttonW, self.fourthBackView.height-buttonY*2) buttonTitle:@"查看详情" color:[UIColor blackTextColor] borderColor:[UIColor grayTextColor]];
        [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:indexPath.section+OrderButtonBaseTag];
        [self.fourthBackView addSubview:button];
    } else {
        [self.secondImageView sd_setImageWithURL:GetUrl(model.gimg) placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];
        [self.secondNameLabel setText:model.gname];
        [self.secondCountLabel setText:[NSString stringWithFormat:@"x%ld",(long)model.ognumber.integerValue]];
        
        NSString *str = [NSString stringWithFormat:@"实付款：¥%@",model.gprice];
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
        [attributedStr setAttributes:@{NSFontAttributeName:TextPingFangMediumFontSize(14),
                                       NSForegroundColorAttributeName:[UIColor ThemeColor]} range:NSMakeRange(0, str.length)];
        [attributedStr setAttributes:@{NSFontAttributeName:TextPingFangFontSize(12),
                                       NSForegroundColorAttributeName:[UIColor ThemeColor]
                                       } range:[str rangeOfString:@"实付款："]];
        [self.secondPriceLabel setAttributedText:attributedStr];
        
        // 属性
        NSString *specs = @"";
        for (NSInteger i = 0; i < model.otop.count; i++) {
            OtopsModel *otops = model.otop[i];
            if (i == 0) {
                specs = otops.taname;
            } else {
                specs = [NSString stringWithFormat:@"%@;%@",specs,otops.taname];
            }
        }
        [self.secondSpecsLabel setText:specs];
    }
}

#pragma mark - Action
- (void)removeFourthBackViewSubView {
    for (UIView *view in self.fourthBackView.subviews) {
        if (view.tag != 10000) {
            [view removeFromSuperview];
        } else {
            NSLog(@"%@",view);
        }
    }
}
#pragma mark - IBAction
- (IBAction)serviceButtonAction:(id)sender {
    if (self.orderBlock) {
        UIButton *button = (UIButton *)sender;
        self.orderBlock(OrderButtonTypeService , button.tag);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
